import { _decorator, Vec3, v3, Node,tween } from 'cc';
import { Util } from './util';
const { ccclass, property } = _decorator;

@ccclass('Room')
export class Room  {
    id:number
    enterPostion: Vec3
    centerPostion: Vec3
    path:Array<Vec3>

    areaPrgPostion: Vec3
    areaPrgWidth: number
    areaPrgHeight: number

    areaPostion: Vec3
    areaWidth: number
    areaHeight: number

    coinCount: number
    roomCoinNode: Node
    doorNode: Node

    clearCoin:boolean = true

    public constructor(id:number, enterPostion: Vec3, centerPostion: Vec3, areaPrgPostion: Vec3, areaPrgWidth: number, areaPrgHeight: number, areaPostion: Vec3, areaWidth: number, areaHeight: number) {
        this.id = id;
        this.enterPostion = enterPostion;
        this.centerPostion = centerPostion;
        this.areaPrgPostion = areaPrgPostion;
        this.areaPrgWidth = areaPrgWidth;
        this.areaPrgHeight = areaPrgHeight;

        this.areaPostion = areaPostion;
        this.areaWidth = areaWidth;
        this.areaHeight = areaHeight;

        this.path = [this.enterPostion, this.centerPostion]
    }

    addPath(...path:Vec3[]) {
        //this.path = [...path, this.enterPostion]
        this.path = [...path, ...this.path]
    }

    getRandomEndPostion():Vec3 {
        return v3(this.areaPrgPostion.x + Util.getRandmNumber(-this.areaPrgWidth/2, this.areaPrgWidth/2), this.areaPrgPostion.y  + Util.getRandmNumber(-this.areaPrgHeight/2, this.areaPrgHeight/2))
    }

    selected(pos: Vec3):boolean {
        if ((pos.x >= this.areaPostion.x - this.areaWidth/2) && (pos.x <= this.areaPostion.x + this.areaWidth/2) &&
        (pos.y >= this.areaPostion.y - this.areaHeight/2) && (pos.y <= this.areaPostion.y + this.areaHeight/2)
        ) {
            return true
        }
        
        return false
    }

    open(func: () => void) {
        if (this.id == 0 || this.id == 3 || this.id == 4 || this.id == 5 || this.id == 7) {
            tween(this.doorNode)
            .to(1,{scale:v3(0, 1)})
            .call(func)
            .start();
        } else {
            tween(this.doorNode)
            .to(1,{scale:v3(1, 0)})
            .call(func)
            .start();
        }
    }


    close(func: () => void) {
        tween(this.doorNode)
        .to(1,{scale:v3(1, 1)})
        .call(func)
        .start();
    }

    coinNumberHint() {
        tween(this.roomCoinNode)
        .then(tween().to(0.5,{scale:v3(1.2, 1.2)}).to(0.5,{scale:v3(1.0, 1.0)})).start();
    }

}

